package com.globalwave.common.event
{
    import flash.display.DisplayObject;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    
    import mx.controls.Alert;
    import mx.core.Application;
  
    public dynamic class KeyHandler {
        private var displayObject : DisplayObject ;
        
        public function KeyHandler(displayObject : DisplayObject = null) {
            if (displayObject == null) {
                this.displayObject = DisplayObject(Application.application) ;
            } else {            
                this.displayObject = displayObject ;
            }
            this.displayObject.addEventListener(KeyboardEvent.KEY_UP, handler);
        }
        
        private function handler(event:KeyboardEvent):void {
            // Alert.show(String.fromCharCode(event.keyCode)) ;
            if (this[event.keyCode]) {
                this[event.keyCode]() ;
            }
            
            if (event.altKey) {
                if (this['A' + event.keyCode]) {
                    this['A' + event.keyCode]() ;
                }
            } else if (event.shiftKey) {
                if (this['S' + event.keyCode]) {
                    this['S' + event.keyCode]() ;
                }
            } else if (event.ctrlKey) {
                if (this['C' + event.keyCode]) {
                    this['C' + event.keyCode]() ;
                }
            }
        }
        

        /*
        private function handler1(event:KeyboardEvent):void {
            //Alert.show( event.keyCode + "/" + event.charCode);
            switch(event.keyCode) {
                case Keyboard.F1:
                    if (this.F1) this['F1']() ;
                    break;
                case Keyboard.F2:
                    if (this.F2) this.F2() ;
                    break;
                case Keyboard.F3:
                    if (this.F3) this.F3() ;
                    break;
                case Keyboard.F4:
                    if (this.F4) this.F4() ;
                    break;
                case Keyboard.F5:
                    if (this.F5) this.F5() ;
                    break;
                case Keyboard.F6:
                    if (this.F6) this.F6() ;
                    break;
                case Keyboard.F7:
                    if (this.F7) this.F7() ;
                    break;
                case Keyboard.F8:
                    if (this.F8) this.F8() ;
                    break;
                case Keyboard.F9:
                    if (this.F9) this.F9() ;
                    break;
                case Keyboard.F10:
                    if (this.F10) this.F10() ;
                    break;
                case Keyboard.F11:
                    if (this.F11) this.F11() ;
                    break;
                case Keyboard.F12:
                    if (this.F12) this.F12() ;
                    break;
                case Keyboard.ESCAPE:
                    if (this.ESC) this.ESC() ;
                    if (this.ESCAPE) this.ESCAPE() ;
                    break;
                case Keyboard.INSERT:
                    if (this.INSERT) this.INSERT() ;
                    if (this.INSERT) this.INSERT() ;
                    break;
                case Keyboard.DELETE:
                    if (this.DEL) this.DEL() ;
                    if (this.DELETE) this.DELETE() ;
                    break;
                case Keyboard.DELETE:
                    if (this.DEL) this.DEL() ;
                    if (this.DELETE) this.DELETE() ;
                    break;
                default:
                    trace("Out of range");
                    break;
            }
            
            if (event.altKey) {
                Alert.show( event.keyCode + "/" + event.charCode );
            }
        }
        public function F1() : void {
            trace("=== key up == >> F1");
        }
        
        public function F2() : void {
            trace("=== key up == >> F2");
        }
        
        public function F3() : void {
            trace("=== key up == >> F3");
        }
        
        public function F4() : void {
            trace("=== key up == >> F4");
        }
        
        public function F5() : void {
            trace("=== key up == >> F5");
        }
        
        public function F6() : void {
            trace("=== key up == >> F6");
        }
        
        public function F7() : void {
            trace("=== key up == >> F7");
        }
        
        public function F8() : void {
            trace("=== key up == >> F8");
        }
        
        public function F9() : void {
            trace("=== key up == >> F9");
        }
        
        public function F10() : void {
            trace("=== key up == >> F10");
        }
        
        public function F11() : void {
            trace("=== key up == >> F11");
        }
        
        public function F12() : void {
            trace("=== key up == >> F12");
        }
        */
        
    }
}